/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller.CoreServlet.Ultility;

import javax.servlet.http.HttpServletRequest;
import Model.ObjectBean.RequestBean.confirm;
import Ultility.CaptchaImage;
import Ultility.RandomCollection;
import java.io.File;
import Model.ObjectBean.RequestBean.BeanRegister;
import java.util.ArrayList;
import java.util.Enumeration;
import Model.DataUltility.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import Model.BeanUltility.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author chickencake1
 */
public class Register {
    public static void sleep(long milisecond)
    {
        long startime =System.currentTimeMillis();
        for(int i=0;i<1000000000;i++)
        {
            long needtime=System.currentTimeMillis()-startime;
            if(needtime==milisecond)
                break;            
        }
    }
    
    public static void MakeCaptchar_toFile(HttpServletRequest request)
    {
       confirm conf =new confirm();
       String captchar=RandomCollection.ChacractersRandom();
       String font=RandomCollection.FontRandom();
       conf.setConfirmcode(captchar);
       request.getSession().setAttribute("conf", conf);

       String url_relative="Resources/CaptchaImage.jpg";
       String url_absolute="C:/Users/chickencake1/Desktop/Java EE/Exercise/Web/Servlet programming/GrrenBookOnline_OnlineProject/web/Resources/CaptchaImage.jpg";
       conf.setUrl(url_relative);
       File file=new File(url_absolute);
       CaptchaImage.writeJPEG(CaptchaImage.makeMessageImage(captchar,font,20),file);
       
    }
    
    public static void MakeCaptchar_response(HttpServletRequest request,HttpServletResponse response)
    {
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
        }
        response.setContentType("image/jpeg");
        
            //PrintWriter out= response.getWriter();

        confirm conf =new confirm();
       String captchar=RandomCollection.ChacractersRandom();
       String font=RandomCollection.FontRandom();
       conf.setConfirmcode(captchar);
       request.getSession().setAttribute("conf", conf);
       String url_relative="Resources/CaptchaImage.jpg";
       conf.setUrl(url_relative);
        try {
            CaptchaImage.writeJPEG(CaptchaImage.makeMessageImage(captchar,font,20),response.getOutputStream());
        } catch (IOException ex) {
            Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
        }
       
    }
    
   /*Function : Check missing value of Fields return List Missing Field Names
    * 
    */
    public static ArrayList checkMissing(HttpServletRequest request)
    {
        
        ArrayList MissingList = new ArrayList();
        if(request.getParameter("payment_style").equals("1"))
        {
            Enumeration paraNames=request.getParameterNames();
            while(paraNames.hasMoreElements())
            {
                String paraName=(String)paraNames.nextElement();
                if(request.getParameter(paraName).equals(""))
                    MissingList.add(paraName);
            }
            return MissingList;
        }
        else
        {
            Enumeration paraNames=request.getParameterNames();
            while(paraNames.hasMoreElements())
            {
                String paraName=(String)paraNames.nextElement();
                if(!paraName.equals("account_ID") && !paraName.equals("postal_code"))
                {
                    if(request.getParameter(paraName).equals(""))
                        MissingList.add(paraName);
                }
            }
            return MissingList;
        }
    }
    
    
    /*Function : Check missing value of Fields return true or false
     * 
     */
    public static boolean checkMissingBoolean(HttpServletRequest request)
    {
        if(request.getParameter("payment_style").equals("1"))
        {
            Enumeration paraNames=request.getParameterNames();
            while(paraNames.hasMoreElements())
            {
                String paraName=(String)paraNames.nextElement();
                if(request.getParameter(paraName).equals(""))
                        return false;
             }
            return true;
        }
        else
        {
             Enumeration paraNames=request.getParameterNames();
            while(paraNames.hasMoreElements())
            {
                String paraName=(String)paraNames.nextElement();

                    if(!paraName.equals("account_ID") && !paraName.equals("postal_code"))
                    {
                    if(request.getParameter(paraName).equals(""))
                        return false;
                    
                    }
                
             }
            return true;
        }
    }
    
    /*Function : Check existing of username in database
     * 
     */
    public static boolean checkUsername(HttpServletRequest request) 
    {
        Data_Access access = new Data_Access();
        String [] colum={"id"};
        String [] data={FilterSpecialCharacters.FilterString(request.getParameter("username"))};
        ResultSet rs= access.getInformation("login_account", colum, data);
        try{
        rs.last();
        int count=rs.getRow();
        if(count==1)
            return true;
        else
            return false;
        }catch(SQLException e)
        {
            return false;
        }
    }
}
